package com.example.surveyapplication;

import help.Define;
import help.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

public class PersonalInfoActivity extends Activity implements OnClickListener {
	Button btnContinue;
	ImageView imvSetting;
	EditText txtFirstName;
	EditText txtSurname;
	EditText txtAge;
	RadioButton rdbFemale;
	RadioButton rdbMale;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personal_info);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		btnContinue = (Button)findViewById(R.id.btnContinue_PersonalInfo);
		imvSetting = (ImageView)findViewById(R.id.imvSetting_PersonalInfo);
		txtFirstName = (EditText)findViewById(R.id.txtFirstname_PersonalInfo);
		txtSurname = (EditText)findViewById(R.id.txtSurname_PersonalInfo);
		txtAge = (EditText)findViewById(R.id.txtAge_PersonalInfo);
		rdbFemale = (RadioButton)findViewById(R.id.rdbFemale_Personallnfo);
		rdbMale = (RadioButton)findViewById(R.id.rdbMale_PersonalInfo);
		rdbMale.setChecked(true);
		
		btnContinue.setOnClickListener(this);
		imvSetting.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.personal_info, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        moveTaskToBack(false);
	        return false;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	public void onClick(View view) {
		Intent intent;
		
		switch (view.getId())
		{
		case R.id.btnContinue_PersonalInfo:
			String firstname = txtFirstName.getText().toString();
			String surname = txtSurname.getText().toString();
			String age = txtAge.getText().toString();
			boolean gender = rdbMale.isChecked();
			
			switch (Utility.createPatient(firstname, surname, age, gender)) {
			case Define.kEmpty:
				showAlert("Please check again.");
				break;
			case Define.kFail:
				showAlert("Sorry, you can't access");
				break;
			case Define.kSuccess:
				intent = new Intent(this, TestActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
			break;
		case R.id.imvSetting_PersonalInfo:
			intent = new Intent(this, ConfirmPasswordActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	private void showAlert(String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("");
		alertDialog.setMessage(message);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				}
				});
		alertDialog.show();
	}
}
